﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginecup.gameState.recycling.Sprite;
using Imaginecup.gameState.recycling.Object.UI;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.recycling.Object.Entity;
using Imaginecup.recycling.Behavior;
using Imaginecup.gameState.recycling.Sprite.Factory;
using Imaginecup.recycling.Object;
using Imaginecup.Recycling.GameMan;
using Imaginecup.Engine;
using Imaginecup.Recycling.Object;
using Imaginecup.gameState.recycling.Observable;
using Imaginecup.recycling.Object.Menu;

namespace Imaginecup.gameState.recycling.Object
{
    public class Pet : IUpdate, IMyObserver
    {
        private float m_CurrentTime = 0;
        private float m_DelayTime = 0.05f;

        private ObjectInfo m_SpriteInfo;
        private Dictionary<CharacterState, IAnimationBehavior> m_AnimationDic;
        private CharacterState m_CurrentState;
        private AnimationSprite m_AnimationSprite;
        private AnimationBehavior m_PetItemAniBehavior;

        public Pet(Rectangle _playerRect)
        {
            m_AnimationDic = new Dictionary<CharacterState, IAnimationBehavior>();
            m_SpriteInfo = new ObjectInfo(_playerRect);
            m_CurrentState = CharacterState.Pet_None;
            Initialize();
        }
        public void AddAnimation(CharacterState state, IAnimationBehavior ani)
        {
            m_AnimationDic.Add(state, ani);
        }
        public void RemoveAnimation(CharacterState state)
        {
            m_AnimationDic.Remove(state);
        }
        public void Release()
        {
            m_AnimationDic.Clear();
        }

        private void Initialize()
        {
            AnimationBehavior runAni = new AnimationBehavior(ResourceManager.GetInstance().GetContent("PET_RUN"), 90);
            runAni.CurrentCharacterState = CharacterState.Pet_None;
            runAni.NextCharacterState = CharacterState.Pet_Eat;
            m_AnimationDic.Add(CharacterState.Pet_None, runAni);

            AnimationBehavior eatAni = new AnimationBehavior(ResourceManager.GetInstance().GetContent("PET_EAT"), 90);
            eatAni.SetLoopAnimationFlag(true);
            eatAni.CurrentCharacterState = CharacterState.Pet_Eat;
            eatAni.NextCharacterState = CharacterState.Pet_None;
            m_AnimationDic.Add(CharacterState.Pet_Eat, eatAni);

            m_PetItemAniBehavior = new AnimationBehavior(ResourceManager.GetInstance().GetContent("PET_GLASS"), 76);

            m_PetItemAniBehavior.Delay = 0.1f;
            eatAni.SetLoopAnimationFlag(true);
            m_AnimationSprite = new AnimationSprite(m_SpriteInfo, m_PetItemAniBehavior);

            Updater.GetInstance().AddUpdate(m_PetItemAniBehavior);
        }
        public void SetPetByPlayer(Vector2 _vector)
        {
            _vector.X -= m_SpriteInfo.FrameWidth + 20;
            _vector.Y -= 30;//m_SpriteInfo.FrameHeight - 5;

            m_SpriteInfo.WorldLocation = _vector;
        }
        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            if (Camera.ObjectIsVisible(m_SpriteInfo.WorldRectangle))
            {
                spriteBatch.Draw(
                    m_AnimationDic[m_CurrentState].GetTexture(),
                    Camera.WorldToScreen(m_SpriteInfo.WorldLocation),
                    m_AnimationDic[m_CurrentState].FrameRectangle,
                    Color.White
                    );
            }
        }

        public void Update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            m_CurrentTime += elapsed;

            if (m_CurrentTime >= m_DelayTime)
            {
                m_CurrentTime = m_CurrentTime - m_DelayTime;
                UpdateAnimation(gameTime);
            }

        }

        private void UpdateAnimation(GameTime gameTime)
        {
            if (m_AnimationDic.ContainsKey(m_CurrentState))
            {
                if (m_AnimationDic[m_CurrentState].GetFinishFlag() && !(m_CurrentState == CharacterState.Pet_Eat))
                {
                    AnimationChange(m_AnimationDic[m_CurrentState].GetNextAnimationKey());
                }
                else
                {
                    m_AnimationDic[m_CurrentState].UpdateAnimation(gameTime);
                }
            }
        }

        private void AnimationChange(CharacterState nextKey)
        {
            m_CurrentState = nextKey;
            m_AnimationDic[m_CurrentState].ResetAnimation();
        }

        public void Update(IObservable ob, object obj)
        {
            ItemData data = (ItemData)obj;

            switch(data.State)
            {
                case Button_State.NONE:
                    RenderDrawer.GetInstance().RemoveSprite(m_AnimationSprite);
                    AnimationChange(CharacterState.Pet_None);
                    break;
                case Button_State.LOCK:
                    RenderDrawer.GetInstance().AddSprite(m_AnimationSprite);
                    AnimationChange(CharacterState.Pet_Eat);
                    m_PetItemAniBehavior.SetTexture(ResourceManager.GetInstance().GetContent("PET_"+data.Name));
                    break;
            }

        }
    }
}
